package com.eshop.utils;

import cn.hutool.core.date.DateTime;
import java.text.SimpleDateFormat;
import cn.hutool.core.util.NumberUtil;
import java.util.Random;
import cn.hutool.core.date.DateUtil;
import com.eshop.enums.ShopCommonEnum;
import java.util.Date;

public class OrderUtil
{
    public static int getSecondTimestamp() {
        final String timestamp = String.valueOf(System.currentTimeMillis() / 1000L);
        return Integer.valueOf(timestamp);
    }
    
    public static String checkActivityStatus(final Date starTime, final Date endTime, final Integer status) {
        final Date nowTime = new Date();
        if (ShopCommonEnum.IS_STATUS_0.getValue().equals(status)) {
            return "关闭";
        }
        if (DateUtil.compare(starTime, nowTime) > 0) {
            return "活动未开始";
        }
        if (DateUtil.compare(endTime, nowTime) < 0) {
            return "活动已结束";
        }
        if (DateUtil.compare(endTime, nowTime) > 0 && DateUtil.compare(starTime, nowTime) < 0) {
            return "正在进行中";
        }
        return "未知";
    }
    
    public static String createShareCode() {
        final int maxNum = 36;
        int count = 0;
        final char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        final StringBuffer pwd = new StringBuffer("");
        final Random r = new Random();
        while (count < 10) {
            final int i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                ++count;
            }
        }
        return pwd.toString();
    }
    
    public static Double randomNumber(final double min, final double max) {
        return NumberUtil.add(min, NumberUtil.mul(Math.random(), NumberUtil.sub(max, min)));
    }
    
    public static String orderSn() {
        final Date date = (Date)DateUtil.date();
        return DateUtil.format(date, "yyyyMMddHHmmssSSS");
    }
    
    public static String stampToDate(final String s) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final long lt = new Long(s) * 1000L;
        final Date date = new Date(lt);
        final String res = simpleDateFormat.format(date);
        return res;
    }
    
    public static Date stampToDateObj(final String s) {
        final long lt = new Long(s) * 1000L;
        final Date date = new Date(lt);
        return date;
    }
    
    public static int getSecondTimestampTwo() {
        final String timestamp = String.valueOf(System.currentTimeMillis() / 1000L);
        return Integer.valueOf(timestamp);
    }
    
    public static int dateToTimestamp(final Date date) {
        final String timestamp = String.valueOf(date.getTime() / 1000L);
        return Integer.valueOf(timestamp);
    }
    
    public static int dateToTimestampT(final DateTime date) {
        final String timestamp = String.valueOf(date.getTime() / 1000L);
        return Integer.valueOf(timestamp);
    }
    
    public static long dateToSecond(final String str) throws Exception {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date date = simpleDateFormat.parse(str);
        final long ts = date.getTime();
        return ts / 1000L;
    }
    
    public static String orderStatusStr(final int paid, final int status, final int shipping_type, final int refund_status) {
        String statusName = "";
        if (paid == 0 && status == 0) {
            statusName = "未支付";
        }
        else if (paid == 1 && status == 0 && shipping_type == 1 && refund_status == 0) {
            statusName = "未发货";
        }
        else if (paid == 1 && status == 0 && shipping_type == 2 && refund_status == 0) {
            statusName = "未核销";
        }
        else if (paid == 1 && status == 1 && shipping_type == 1 && refund_status == 0) {
            statusName = "待收货";
        }
        else if (paid == 1 && status == 1 && shipping_type == 2 && refund_status == 0) {
            statusName = "未核销";
        }
        else if (paid == 1 && status == 2 && refund_status == 0) {
            statusName = "待评价";
        }
        else if (paid == 1 && status == 3 && refund_status == 0) {
            statusName = "已完成";
        }
        else if (paid == 1 && refund_status == 1) {
            statusName = "退款中";
        }
        else if (paid == 1 && refund_status == 2) {
            statusName = "已退款";
        }
        return statusName;
    }
    
    public static int orderStatus(final int paid, final int status, final int refund_status) {
        int _status = 0;
        if (paid == 0 && status == 0 && refund_status == 0) {
            _status = 1;
        }
        else if (paid == 1 && status == 0 && refund_status == 0) {
            _status = 2;
        }
        else if (paid == 1 && refund_status == 1) {
            _status = 3;
        }
        else if (paid == 1 && status == 1 && refund_status == 0) {
            _status = 4;
        }
        else if (paid == 1 && status == 2 && refund_status == 0) {
            _status = 5;
        }
        else if (paid == 1 && status == 3 && refund_status == 0) {
            _status = 6;
        }
        else if (paid == 1 && refund_status == 2) {
            _status = 7;
        }
        return _status;
    }
    
    public static String payTypeName(final String pay_type, final int paid) {
        String payTypeName = "";
        if (paid == 1) {
            switch (pay_type) {
                case "weixin": {
                    payTypeName = "微信支付";
                    break;
                }
                case "yue": {
                    payTypeName = "余额支付";
                    break;
                }
                case "integral": {
                    payTypeName = "积分兑换";
                    break;
                }
                case "offline": {
                    payTypeName = "线下支付";
                    break;
                }
                default: {
                    payTypeName = "其他支付";
                    break;
                }
            }
        }
        else {
            switch (pay_type) {
                default: {
                    payTypeName = "未支付";
                    break;
                }
                case "offline": {
                    payTypeName = "线下支付";
                    break;
                }
            }
        }
        return payTypeName;
    }
    
    public static String payChannel(final Integer pay_channel) {
        if (pay_channel.equals(1)) {
            return "微信小程序";
        }
        return "微信公众号";
    }
    
    public static String orderType(final int pinkId) {
        return "普通订单";
    }
}
